package com.meichis.yndms.ui.fragment;


import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.meichis.mcsappframework.adapter.recyclerview.MultiItemTypeAdapter;
import com.meichis.mcsappframework.dialog.DropDownDialog;
import com.meichis.mcsappframework.utils.DateUtil;
import com.meichis.mcsappframework.utils.SharePreferenceUtil;
import com.meichis.yndms.R;
import com.meichis.yndms.adapter.NoticeAdapter;
import com.meichis.yndms.common.MCode;
import com.meichis.yndms.databinding.FragmentNoticeBinding;
import com.meichis.yndms.model.Notice;
import com.meichis.yndms.ui.activity.NoticeDetailActivity;
import com.meichis.yndms.ui.common.BaseFragment;
import com.meichis.yndms.vm.NoticeListVM;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class NoticeFragment extends BaseFragment {

    private FragmentNoticeBinding binding;
    private NoticeListVM vm;
    private NoticeAdapter adapter;

    @Override
    protected View onCreateDataBindingView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_notice, container, false);
        vm = new ViewModelProvider(getActivity(), new ViewModelProvider.NewInstanceFactory()).get(NoticeListVM.class);
        binding.setVm(vm);
        binding.setClick(new ClickEvent());
        binding.setLifecycleOwner(this);
        return binding.getRoot();
    }

    @Override
    protected void init() {
        binding.tvUnRead.setSelected(true);
        adapter = new NoticeAdapter(getActivity(), R.layout.item_notice, new ArrayList<>());
        binding.rcNotice.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.rcNotice.setHasFixedSize(true);
        binding.rcNotice.setAdapter(adapter);
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, Object o, int position) {
                if (!adapter.getDatas().get(position).isNoticeRead()) {
                    adapter.getDatas().get(position).setNoticeRead(true);
                    vm.UpdateNoticeRead(adapter.getDatas().get(position).getNoticeId());
                    util.setBooleanValue(SharePreferenceUtil.PREFERENCES_REFRESH, true);
                } else {
                    //当前点击页是已读界面
                    vm.isReadPage = true;
                }
                Intent intent = new Intent();
                intent.setClass(getActivity(), NoticeDetailActivity.class);
                intent.putExtra(MCode.NOTICE, adapter.getDatas().get(position));
                intent.putExtra(MCode.TYPE, adapter.getDatas().get(position).getNoticeType());
                intent.putExtra(MCode.TITLE, adapter.getDatas().get(position).getNoticeTitle());
                intent.putExtra(MCode.DATE, adapter.getDatas().get(position).getPublishDate());
                startActivity(intent);

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, Object o, int position) {
                return false;
            }
        });
        addObserver();
        vm.GetNotice();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (util.getBooleanValue(SharePreferenceUtil.PREFERENCES_REFRESH, false)) {
            util.remove(SharePreferenceUtil.PREFERENCES_REFRESH);
            //修改数据堆加
            vm.getReadNoticeList().getValue().clear();
            vm.getUnReadNoticeList().getValue().clear();
            vm.GetNotice();
        } else if (vm.getUnReadNoticeList().getValue().size() > 0 || !vm.isReadPage) {
            //只要不是从已读界面跳转到详情界面的任何界面回到此界面，都默认打卡未读界面
            binding.tvUnRead.setSelected(true);
            binding.tvRead.setSelected(false);
            setList(vm.getUnReadNoticeList().getValue());
        }
        vm.isReadPage = false;
    }

    private void addObserver() {
        vm.getShowDialog().observe(this, aBoolean -> showLoading(aBoolean));

        vm.getNoticeList().observe(this, notices -> {
            vm.getReadNoticeList().getValue().clear();
            vm.getUnReadNoticeList().getValue().clear();
            for (Notice notice : notices) {
                if (notice.isNoticeRead()) {
                    vm.getReadNoticeList().getValue().add(notice);
                } else {
                    vm.getUnReadNoticeList().getValue().add(notice);
                }
            }
            if (binding.tvUnRead.isSelected()) {
                setList(vm.getUnReadNoticeList().getValue());
            } else {
                setList(vm.getReadNoticeList().getValue());
            }

        });
    }

    private void setList(List<Notice> notices) {
        adapter.setList(notices);
        if (notices.size() == 0) {
            binding.rcNotice.setVisibility(View.GONE);
            binding.tvNothing.setVisibility(View.VISIBLE);
        } else {
            binding.rcNotice.setVisibility(View.VISIBLE);
            binding.tvNothing.setVisibility(View.GONE);
        }
    }

    public class ClickEvent {

        public void onSelectNoticeType() {
            new DropDownDialog<>(getActivity(), R.color.theme, "类型", "Name", vm.getNoticeTypeList(), new DropDownDialog.SelectedListener() {
                @Override
                public void setSelect(Object o, int position) {
                    vm.getNoticeType().setValue(vm.getNoticeTypeList().get(position));
                    vm.GetNotice();
                }
            }).show();
        }

        public void selectBeginTime() {
            final Calendar cd = Calendar.getInstance();
            new DatePickerDialog(getActivity(), AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    cd.set(Calendar.YEAR, year);
                    cd.set(Calendar.MONTH, monthOfYear);
                    cd.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    vm.getBeginTime().setValue(DateUtil.date2Str(cd, DateUtil.FORMAT_YMD));
                }
            }, cd.get(Calendar.YEAR), cd.get(Calendar.MONTH), cd.get(Calendar.DAY_OF_MONTH)).show();
        }

        public void selectEndTime() {
            final Calendar cd = Calendar.getInstance();
            new DatePickerDialog(getActivity(), AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    cd.set(Calendar.YEAR, year);
                    cd.set(Calendar.MONTH, monthOfYear);
                    cd.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    vm.getEndTime().setValue(DateUtil.date2Str(cd, DateUtil.FORMAT_YMD));
                }
            }, cd.get(Calendar.YEAR), cd.get(Calendar.MONTH), cd.get(Calendar.DAY_OF_MONTH)).show();
        }

        public void searchNotice() {
            vm.GetNotice();
        }

        public void onUnRead() {
            binding.tvUnRead.setSelected(true);
            binding.tvRead.setSelected(false);
            setList(vm.getUnReadNoticeList().getValue());
        }

        public void onRead() {
            binding.tvUnRead.setSelected(false);
            binding.tvRead.setSelected(true);
            setList(vm.getReadNoticeList().getValue());
        }
    }
}
